package com.jiantong.user.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.user.model.AppUserRole;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 4/7/17.
 *
 */
@Repository
public class AppUserRoleDao extends MyBatisDao<AppUserRole>{

    /**
     * 根据用户id查询
     * @param userId 用户id
     * @return List<AppUserRole>
     */
    public List<AppUserRole> findAppUserRoles(Long userId){
        return getSqlSession().selectList(sqlId("findAppUserRolesByUserId"),userId);
    }

    public Boolean updateByUserId(AppUserRole appUserRole){
        return getSqlSession().update(sqlId("updateByUserId"),appUserRole) == 1;
    }

    public AppUserRole findByUserId(Long userId){
        return getSqlSession().selectOne(sqlId("findByUserId"),userId);
    }

    public Paging<AppUserRole> pagingLaw(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = (Long)this.sqlSession.selectOne(this.sqlId("countLaw"), criteria);
        if(total.longValue() <= 0L) {
            return new Paging(Long.valueOf(0L), Collections.emptyList());
        } else {
            ((Map)criteria).put("offset", offset);
            ((Map)criteria).put("limit", limit);
            List datas = this.sqlSession.selectList(this.sqlId("pagingLaw"), criteria);
            return new Paging(total, datas);
        }
    }


}
